﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for PhieuPhat.xaml
    /// </summary>
    public partial class PhieuPhat : Window
    {

        THUVIEN.Service1 serv = new THUVIEN.Service1();
        XuLyThem m_XulyThem = new XuLyThem();
        int ConLai = 0;
        string m_MaDocGia;

        public PhieuPhat()
        {
            InitializeComponent();
            HienThi();
        }

        private void bntPTOk_Click(object sender, RoutedEventArgs e)
        {
            //Cap nhật lai so tien doc gia thieu.
            if (txtPTTienThu.Text=="")
            {
                MessageBox.Show("Vui lòng nhập số tiền thu", "Thông báo");
            }
            else
            {
                serv.UpdateDocGia_TienNo(m_MaDocGia, ConLai);
                serv.InsertPhieuPhat(m_MaDocGia, DateTime.Now, TraSach.TongNo, int.Parse(txtPTTienThu.Text), ConLai);
                TraSach.TongNo = ConLai;
                this.Close();
            }
        }

        private void HienThi()
        {
            m_MaDocGia = serv.SelectPhieuTra_MaDocGia(TraSach.m_MaPhieuTra);
            txtPTHoTenDG.Text = serv.SelectTenDocGia(m_MaDocGia);
            txtPTTienNo.Text = TraSach.TongNo.ToString();
            txtPTConLai.Text = ConLai.ToString();
            
        }
       
        private void txtPTTienThu_TextChanged(object sender, TextChangedEventArgs e)
        {
            int TienThu;
            bool check, check1;
            check = m_XulyThem.KiemTraTextBox(txtPTTienThu.Text);
            
            //Kiem tra xem textbox co rong hay ko
            if (!check)
            {
                TienThu = 0;
            }

            else
            {
                check1 = m_XulyThem.KiemTra(txtPTTienThu.Text);
               //kiem tra xem vua nhap co la so hay ko
                if (check1)
                {
                    TienThu = int.Parse(txtPTTienThu.Text);
                    //Kiem tra tien thu co hon tien phat hay ko
                    if (TienThu > TraSach.TongNo) 
                    {
                        MessageBox.Show("Tiền thu không được vượt quá số tiền độc giả đang nợ", "Thông báo");
                        txtPTTienThu.Text = "";
                        ConLai = 0;
                    }

                    else
                    {
                        //neu khong còn lai tinh nhu sau.
                        ConLai = TraSach.TongNo - TienThu;
                        txtPTConLai.Text = ConLai.ToString();
                    }  
                }

                else
                {
                    MessageBox.Show("Vui lòng nhập số", "Thông báo");
                    txtPTTienThu.Text = "";
                }
            }
        }
    }
}
